$(function () {
    $("#jqGrid").jqGrid({
        url: '/platform/manage/menulist',
        datatype: "json",
        colModel: [
			{ label: '菜单ID', name: 'menuId', index: "menuId", width: 40, key: true,hidden:true },
			{ label: '菜单名称', name: 'menuName', width: 60 },
			{ label: '上级菜单', name: 'menuUpName', sortable: false, width: 60 },
			{ label: '菜单图标', name: 'menuIcon', sortable: false, width: 50, formatter: function(value, options, row){
				return value == null ? '' : '<i class="'+value+' fa-lg"></i>';
			}},
			{ label: '菜单URL', name: 'menuUrl', width: 100 },
			{ label: '授权标识', name: 'menuPerms', width: 100 },
			{ label: 'OEM可用', name: 'oemUse', width: 40,formatter:function (value) {
				return value==1?"否":"是";
			} },
			{ label: '类型', name: 'menuType', width: 50, formatter: function(value, options, row){
				if(value === 0){
					return '<span class="label label-primary">目录</span>';
				}
				if(value === 1){
					return '<span class="label label-success">菜单</span>';
				}
				if(value === 2){
					return '<span class="label label-warning">按钮</span>';
				}
			}}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "result.list",
            page: "result.currPage",
            total: "result.totalPage",
            records: "result.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var setting = {
	data: {
		simpleData: {
			enable: true,
			idKey: "menuId",
			pIdKey: "menuUpId",
			rootPId: -1
		},
		key: {
			url:"nourl",
			name:"menuName",
            // children:"list"
		}
	}
};
var ztree;
var ztreeLyerIndex;

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		menu:{
            menuUpName:null,
            menuUpId:0,
            menuType:1,
			orderNum:0
		}
	},
	methods: {
		getMenu: function(menuId){
			//加载菜单树
			$.get("/platform/manage/allmenutree", function(r){
				ztree = $.fn.zTree.init($("#menuTree"), setting, vm.handelTreeData(r.result));
				if (vm.menu.menuUpId == 0)
					return;
				var node = ztree.getNodeByParam("menuId", vm.menu.menuUpId);
				ztree.selectNode(node);
				vm.menu['menuUpName'] = node.menuName;
			})
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.menu = {menuUpName:null,menuUpId:0,menuType:1,orderNum:0};
			vm.getMenu();
		},
		handelTreeData:function (data) {
			var datas = [];
			for (var i = 0;i<data.length;i++){
				var menu = data[i];
				if (menu.menuType == 2)
					continue;
				if (menu.list != null && menu.list.length > 0){
                    var sub = vm.handelTreeData(menu.list);
                    datas.unshift.apply(datas,sub);
				}
				datas.push(menu);
			}
			return datas;
        },
		update: function (event) {
			var menuId = getSelectedRow();
			if(menuId == null){
				return ;
			}
			
			$.get("/platform/manage/menuinfo/"+menuId, function(r){
				vm.showList = false;
                vm.title = "修改";
                vm.menu = r.result;
                vm.getMenu();
            });
		},
		del: function (event) {
			var menuIds = getSelectedRow();
			if(menuIds == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
				    url: "/platform/manage/menudel/"+menuIds,
				    success: function(r){
				    	if(r.code == 200){
							alert('操作成功', function(index){
								vm.reload();
							});
						}else{
							alert(r.message);
						}
					}
				});
			});
		},
		saveOrUpdate: function (event) {
			var url = vm.menu.menuId == null ? "/platform/manage/menuadd" : "/platform/manage/menuupdate";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.menu),
			    success: function(r){
					console.log(r)
			    	if(r.code == 200){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.message);
					}
				}
			});
		},
		menuTree: function(){
            ztree.expandAll(true);
			ztreeLyerIndex = layer.open({
				type: 1,
				offset: '50px',
				skin: 'layui-layer-molv',
				title: "选择菜单",
				area: ['300px', '450px'],
				shade: 0,
				shadeClose: false,
				content: jQuery("#menuLayer"),
				btn: ['确定', '取消'],
				btn1: function (index) {
					var node = ztree.getSelectedNodes();
					//选择上级菜单
					vm.menu.menuUpId = node[0].menuId;
					vm.menu.menuUpName = node[0].menuName;
					
					layer.close(index);
	            }
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			layer.close(ztreeLyerIndex);
			$("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
		}
	}
});